﻿@inherits OwningComponentBase<ICovenantService>

@using System.Collections.Generic
@using Microsoft.JSInterop

@using Covenant.Core
@using Covenant.Models.Grunts
@using Covenant.Components.Shared
@inject IJSRuntime IJSRuntime
@inject INotificationService INotificationService

<CovenantTable TItem="Grunt" Items="Grunts" ItemsChanged="GruntsChanged" SearchRow="IsRowDisplayed"
               IsSearchable="IsSearchable" IsSortable="IsSortable" IsPaginated="IsPaginated" PageLength="PageLength" class="table table-hover">
    <TableHeader>
        <SortableTableHeader TItem="Grunt" TItem2="string" GetSortableProperty="(grunt) => grunt.Name">
            <HeaderContent>Name</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="Grunt" TItem2="string" GetSortableProperty="(grunt) => grunt.ImplantTemplate.Name">
            <HeaderContent>ImplantTemplate</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="Grunt" TItem2="string" GetSortableProperty="(grunt) => grunt.Hostname">
            <HeaderContent>Hostname</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="Grunt" TItem2="string" GetSortableProperty="(grunt) => grunt.UserName">
            <HeaderContent>UserName</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="Grunt" TItem2="GruntStatus" GetSortableProperty="(grunt) => grunt.Status">
            <HeaderContent>Status</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="Grunt" TItem2="DateTime" GetSortableProperty="(grunt) => grunt.LastCheckIn">
            <HeaderContent>LastCheckIn</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="Grunt" TItem2="IntegrityLevel" GetSortableProperty="(grunt) => grunt.Integrity">
            <HeaderContent>Integrity</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="Grunt" TItem2="string" GetSortableProperty="(grunt) => grunt.Note">
            <HeaderContent>Note</HeaderContent>
        </SortableTableHeader>
    </TableHeader>
    <TableRow Context="grunt">
        <tr style="opacity: @GetGruntStatusOpacity(grunt.Status)">
            <td @attributes="InputAttributes">
                <a href="/grunt/interact/@grunt.Id">
                    @grunt.Name
                </a>
            </td>
            <td @attributes="InputAttributes">@grunt.ImplantTemplate.Name</td>
            <td @attributes="InputAttributes">@grunt.Hostname</td>
            <td @attributes="InputAttributes">@grunt.UserName</td>
            <td @attributes="InputAttributes">@grunt.Status</td>
            <td @attributes="InputAttributes">@grunt.LastCheckIn</td>
            <td @attributes="InputAttributes">@grunt.Integrity</td>
            <td @attributes="InputAttributes">@grunt.Note</td>
        </tr>
    </TableRow>
    <ButtonTray>@ButtonTray</ButtonTray>
</CovenantTable>

@code {
    [Parameter]
    public List<Grunt> Grunts { get; set; }

    [Parameter]
    public EventCallback<List<Grunt>> GruntsChanged { get; set; }

    [Parameter]
    public bool IsSearchable { get; set; } = false;

    [Parameter]
    public bool IsSortable { get; set; } = true;

    [Parameter]
    public bool IsPaginated { get; set; } = true;

    [Parameter]
    public int PageLength { get; set; } = 50;

    [Parameter]
    public RenderFragment ButtonTray { get; set; }

    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object> InputAttributes { get; set; }

    protected async override Task OnInitializedAsync()
    {
        this.Grunts = this.Grunts ?? (await Service.GetGrunts()).ToList();
    }

    private string GetGruntStatusOpacity(GruntStatus s)
    {
        return s == GruntStatus.Active ? "1.0" : "0.3";
    }

    private void OnGruntsChanged(List<Grunt> Grunts)
    {
        this.Grunts = Grunts;
        this.GruntsChanged.InvokeAsync(Grunts);
    }

    private bool IsRowDisplayed(Grunt grunt, string SearchTerm)
    {
        return SearchTerm == string.Empty ||
            grunt.Name.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            grunt.ImplantTemplate.Name.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            grunt.Hostname.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            grunt.UserName.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            grunt.Status.ToString().Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            grunt.LastCheckIn.ToString().Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            grunt.Integrity.ToString().Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            grunt.OperatingSystem.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            grunt.Process.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase);
    }
}